/**
 * Created Date: Saturday, April 8th 2023, 4:34:44 pm
 * Author: CodingGorit
 * -----
 * Last Modified: Fri Apr 21 2023
 * Modified By: CodingGorit
 * -----
 * Copyright © 2019 —— 2023 fmin-courses All Rights Reserved
 * ------------------------------------
 * Javascript will save your soul!
 */


import { type } from 'os';
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';

// setup stores => https://pinia.vuejs.org/core-concepts/#setup-stores
// more simplify usage => https://github.com/vuejs/pinia/issues/978
export const useAdminStore = defineStore("admin", () => {
    // STATES
    const adminId = ref('');
    const loginState = ref(false);
    const avatar = ref<string>("");
    const signature = ref("");

    // GETTERS
    const getAvatar = computed(() => {
        return avatar.value;
    });

    // ACTIONS
    function changeLoginState(state: boolean) {
        if (state !== loginState.value) {
            loginState.value = state;
        }
    }

    function setAvatar(url: string) {
        avatar.value = url;
    }

    function setSignature(sign: string) {
        signature.value = sign;
    }

    function setAdminId(id: string) {
        adminId.value = id;
    }

    function getAdminId() {
        return adminId.value;
    }

    function $reset() {
        loginState.value = false;
        avatar.value = '';
        signature.value = '';
    }
    return {
        loginState,
        avatar,
        signature,
        setAdminId,
        getAdminId,
        changeLoginState,
        setAvatar,
        getAvatar,
        setSignature,
        $reset,
    }
}, {
    // 默认情况下，每个声明的 Store 中的 persist: true 配置都会将数据持久化到sessionStorage 中
    persist: [{
        paths: ['loginState'],
        storage: sessionStorage
    }]
});